package com.yq.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yq.constants.Constants;
import com.yq.domain.DictType;
import com.yq.dto.DictTypeDto;
import com.yq.mapper.DictTypeMapper;
import com.yq.servicce.DictTypeService;
import com.yq.vo.DataGridView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;

/**
* @Author: yangQ
* @Date: 2020/11/24 18:54
* @Description:
*/
@Service
public class DictTypeServiceImpl implements DictTypeService{

    @Autowired
    private DictTypeMapper dictTypeMapper;

    /**
     * 分页查询字典类型数据
     * @param dictTypeDto
     * @return
     */
    @Override
    public DataGridView listPage(DictTypeDto dictTypeDto) {
        Page<DictType> page = new Page<>(dictTypeDto.getPageNum(),dictTypeDto.getPageSize());
        QueryWrapper<DictType> qw = new QueryWrapper<>();
        qw.like(StringUtils.isNotBlank(dictTypeDto.getDictName()),DictType.COL_DICT_NAME,dictTypeDto.getDictName());
        qw.like(StringUtils.isNotBlank(dictTypeDto.getDictType()),DictType.COL_DICT_TYPE,dictTypeDto.getDictType());
        qw.eq(StringUtils.isNotBlank(dictTypeDto.getStatus()),DictType.COL_STATUS,dictTypeDto.getStatus());
        qw.ge(null!=dictTypeDto.getBeginTime(),DictType.COL_CREATE_TIME,dictTypeDto.getBeginTime());
        qw.le(null!=dictTypeDto.getEndTime(),DictType.COL_CREATE_TIME,dictTypeDto.getEndTime());
        this.dictTypeMapper.selectPage(page,qw);
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    /**
     * 查询所有的字典类型
     * @return
     */
    @Override
    public DataGridView list() {
        QueryWrapper<DictType> qw = new QueryWrapper<>();
        qw.eq(DictType.COL_STATUS, Constants.STATUS_TRUE);
        return new DataGridView(null,this.dictTypeMapper.selectList(qw));
    }

    /**
     * 检查字典类型唯一性
     * @param dictId
     * @param dictType
     * @return
     */
    @Override
    public Boolean checkDictTypeUnique(Long dictId, String dictType) {
//        dictId = (dictId == null) ? -1L : dictId;
        QueryWrapper<DictType> qw=new QueryWrapper<>();
        qw.eq(DictType.COL_DICT_TYPE, dictType);
        DictType sysDictType = this.dictTypeMapper.selectOne(qw);
        if(null!=sysDictType){
            return true;
        }
        return false;
    }

    /**
     * 添加一个字典类型数据
     * @param dictTypeDto
     * @return
     */
    @Override
    public int insert(DictTypeDto dictTypeDto) {
        DictType dictType =new DictType();
        BeanUtils.copyProperties(dictTypeDto,dictType);
        dictType.setCreateBy(dictTypeDto.getSimpleUser().getUserName());
        dictType.setCreateTime(DateUtil.date());
        return dictTypeMapper.insert(dictType);
    }

    /**
     * 更新字典数据类型
     * @param dictTypeDto
     * @return
     */
    @Override
    public int update(DictTypeDto dictTypeDto) {
        DictType dictType =new DictType();
        BeanUtils.copyProperties(dictTypeDto,dictType);
        dictType.setUpdateBy(dictTypeDto.getSimpleUser().getUserName());
        dictType.setUpdateTime(DateUtil.date());
        return dictTypeMapper.updateById(dictType);
    }

    /**
     * 批量删除字典类型
     * @param dictIds
     * @return
     */
    @Override
    public int deleteDictTypeByIds(Long[] dictIds) {
        if(null!=dictIds&&dictIds.length>0) {
            return this.dictTypeMapper.deleteBatchIds(Arrays.asList(dictIds));
        }else {
            return -1;
        }
    }

    /**
     * 根据ID查询一个字典类型
     * @param dictId
     * @return
     */
    @Override
    public DictType selectDictTypeById(Long dictId) {
        return this.dictTypeMapper.selectById(dictId);
    }
}
